package com.p2p.model;

import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.p2p.constant.ErrorConstants;

/**
 * PeerJS消息模型类，对应FRONTEND_INTERFACE.md中定义的消息格式
 * 所有消息通过WebSocket以JSON格式传输
 */
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class PeerMessage {

    /**
     * 消息类型，对应文档中的type字段
     * 可能的值：OPEN、LEAVE、CANDIDATE、OFFER、ANSWER、EXPIRE、HEARTBEAT、ID_TAKEN、ERROR
     */
    private String type;

    /**
     * 发送方ID，对应文档中的src字段
     */
    private String peerId;

    /**
     * 接收方ID，对应文档中的dst字段
     */
    private String targetId;

    /**
     * 消息内容，对应文档中的payload字段
     * 根据消息类型不同，payload格式不同
     */
    private JsonNode payload;

    // Getters and Setters
    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getPeerId() {
        return peerId;
    }

    public void setPeerId(String peerId) {
        this.peerId = peerId;
    }

    public String getTargetId() {
        return targetId;
    }

    public void setTargetId(String targetId) {
        this.targetId = targetId;
    }

    public JsonNode getPayload() {
        return payload;
    }

    public void setPayload(JsonNode payload) {
        this.payload = payload;
    }

    @Override
    public String toString() {
        return "PeerMessage{" +
                "type='" + type + "'" +
                ", peerId='" + peerId + "'" +
                ", targetId='" + targetId + "'" +
                ", payload=" + payload +
                '}';
    }

    // 静态工厂方法，用于创建不同类型的消息
    private static final ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 创建错误消息
     * @param errorCode 错误码
     * @param errorMessage 错误消息
     * @return 错误消息对象
     */
    public static PeerMessage createErrorMessage(String errorCode, String errorMessage) {
        PeerMessage message = new PeerMessage();
        message.setType("ERROR");
        Map<String, String> payload = new HashMap<>();
        payload.put("code", errorCode);
        payload.put("message", errorMessage);
        try {
            message.setPayload(objectMapper.valueToTree(payload));
        } catch (Exception e) {
            // 处理异常
        }
        return message;
    }

    /**
     * 创建ID已被占用消息
     * @param id 被占用的ID
     * @return ID已被占用消息对象
     */
    public static PeerMessage createIdTakenMessage(String id) {
        PeerMessage message = new PeerMessage();
        message.setType("ID_TAKEN");
        Map<String, String> payload = new HashMap<>();
        payload.put("id", id);
        try {
            message.setPayload(objectMapper.valueToTree(payload));
        } catch (Exception e) {
            // 处理异常
        }
        return message;
    }

    /**
     * 创建消息过期通知
     * @param targetId 目标客户端ID
     * @return 消息过期通知对象
     */
    public static PeerMessage createExpireMessage(String targetId) {
        PeerMessage message = new PeerMessage();
        message.setType("EXPIRE");
        Map<String, String> payload = new HashMap<>();
        payload.put("dst", targetId);
        try {
            message.setPayload(objectMapper.valueToTree(payload));
        } catch (Exception e) {
            // 处理异常
        }
        return message;
    }

}